<?php 
namespace Admin\Model;
use Think\Model;

class NewsModel extends Model
{	

	public function addNews_add()
	{
		$news = D('news');
		$data = I('post.');
		$data['addtime'] = time();
		$map['id'] = session('admin_id');
		// $data['content'] = htmlspecialchars($data['content']);
		$m = M('admin');
		$list = $m->where($map)->find();

		$data['author'] = $list['name'];

		if(empty(trim($data['title']))){
			return 'title';
		}

		if(empty(trim($data['content']))){
			return 'content';
		}

		$res = $this->add($data);
		if($res){
			return 'success';
		}else{
			return 'error';
		}
	}
	//读取要修改的新闻内容并展现在修改页面
	public function news_edit()
	{
		$id = I('get.id');
		$map['id'] = ['eq',$id];

		$list = $this->where($map)->find();
		return $list;
	}
	//修改新闻内容
	public function edit_Mod()
	{
		$data = I('post.');
		$m = M('admin');
		$map['id'] = ['eq',session('admin_id')];
		$list = $m->where($map)->find();

		if(empty(trim($data['title']))){
			return 'title';
		}

		if(empty(trim($data['content']))){
			return 'content';
		}
		$data['author'] = $list['name'];
		$data['addtime'] = time();
		
		$where['id'] = ['eq',$data['id']];
		$res = $this->where($where)->save($data);
		if($res){
			return 'success';
		}
	}
}